--
-- Author: ping.wang
-- Date: 2018-07-12 16:07:16
--
-- AuctionKeyboardPanel
--

local ClassRef = app.mvc.createPanelClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.AuctionKeyboard))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

function ClassRef:init(data)
    self.super.init(self, data)
    self:setContentSize( self._ui.Panel:getContentSize() )
    for i = 0,9 do
        self._ui["btn_"..i]:addClickEventListener(function() self:_clickNum(i) end)
    end

    self._curNum = 0
    self._numMin = 0
    self._numMax = 999999999
    self._showNumText = self._ui.bg:findChild("name")

    self._ui.btn_ok:addClickEventListener(function()
        executeFunc(self._callFunc,self._curNum)
        executeFunc(self._onOperate, "closeMiniKeyboard", self._curNum)
    end)
    self._ui.btn_del:addClickEventListener(handler( self, self._clickDel ) )
end

function ClassRef:_clickNum(num)
    local val = self._curNum * 10 + tonumber(num)
    self:setNum(val)
    --self._curNum = executeFunc(self._callFunc,self._curNum)
end

function ClassRef:_clickDel()
    if self._curNum > 0 then
        local val =  math.floor(self._curNum /10)
        self:setNum(val)
        --self._curNum = executeFunc(self._callFunc,self._curNum)
    end
end

function ClassRef:setShowNumText(b)
    self._ui.bg:setVisible(b)
end

function ClassRef:setNumLimit(numMin, numMax)
    self._numMin = numMin
    self._numMax = numMax
end

function ClassRef:getPanel()
    return self._ui.Panel
end

function ClassRef:close()
    self:removeFromParent()
end

function ClassRef:setNum(num)
    -- 判断一下最大最小值
    num = math.min(tonumber(num), self._numMax)
    num = math.max(num, self._numMin)

    if self._curNum ~= num or num == 0 then
        self._curNum = num
        if self._ui.bg:isVisible() then
            self._showNumText:setString(self._curNum)
        end

        executeFunc(self._onOperate, "numChange", self._curNum)
    end
end

function ClassRef:setCallFunc(cb)
    self._callFunc = cb
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

return ClassRef
